import ast
import os
import re

from config.config import path
from untils.log_ import logger

class ProcessData:
    @staticmethod
    def write_yaml(data, filename):
        import yaml
        filename = path + os.sep + filename + '.yaml'
        logger.info('write_yaml: %s', filename)
        with open(filename, 'w') as f:
            yaml.dump(data, f)

    @staticmethod
    def read_yaml(filename):
        import yaml
        filename = path + os.sep + filename + '.yaml'
        with open(filename, 'r') as f:
            return yaml.safe_load(f)

    @staticmethod
    def read_excel(filename):
        """

        :param filename:
        :return:
        """
        import openpyxl
        book = openpyxl.load_workbook(filename)
        sheet = book.active
        cells = sheet["G11":"H40"]
        data = {"data": [], "expect": []}
        test_data = [data]
        for v in cells:
            data = {"data": [], "expect": []}
            data['data'] = ast.literal_eval(v[0].value)
            match = re.search(r'{.*}', v[1].value, re.DOTALL)
            if match:
                json_data = match.group(0)
                data['expect'] = ast.literal_eval(json_data)
            else:
                data['expect'] = {}
                logger.debug(f"未找到匹配的JSON数据 : {v[1].value} 加入空数据")
            test_data.append(data)

        return test_data


if __name__ == '__main__':
    print(ProcessData.read_excel(path + os.sep + r'../data/部门管理接口测试用例.xlsx'))
